package org.sensornet2010.co2sh.core;

import java.io.File;
import java.util.Date;
import android.media.MediaRecorder;

public class SoundRecorder {

	protected long duration;
	public static String	DIRECTORY	= "/sdcard/tmp/";

	public SoundRecorder(long duration) {
		this.duration = SensornetConfiguration.RECORDING_DURATION;
	}
	
	public byte[] record() throws Exception {
		SensornetUtil.debug("SoundRecorder:record()");
		String path = this.recordToFile();
		File file = new File(path);
		byte[] sound = SensornetUtil.getBytesFromFile(file);
		int len = sound.length;
		if (len < 1) throw new SensornetException(null,SensornetException.SOUND_RECORDING_ERROR, "record():0 size length" + len); 
		return sound;
	
	}
	private String recordToFile(){
		SensornetUtil.debug("SoundRecorder:recordToFile() start");
		MediaRecorder mediaRecorder = new MediaRecorder();
		Date date = new Date();
		String fileFullPath = DIRECTORY + date.getTime()+".3gp";
		
		// TODO directory initialization
		// make sure SD card is ready
		String state = android.os.Environment.getExternalStorageState();
		if (!state.equals(android.os.Environment.MEDIA_MOUNTED))
			throw new RuntimeException("SD Card is not mounted.  It is " + state + ".");
		
		// make sure the directory we plan to store the recording in exists
		File directory = new File(fileFullPath).getParentFile();
		if (!directory.exists() && !directory.mkdirs())
			throw new RuntimeException("Path to file created.");
		
		mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
		mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
		mediaRecorder.setOutputFile( fileFullPath);
		try {
			mediaRecorder.prepare();
			mediaRecorder.start();
			Thread.sleep(duration);
			mediaRecorder.stop();
			mediaRecorder.release();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			// TODO make sure that resources are released
		}
		SensornetUtil.debug("SoundRecorder:recordToFile() done");
		return fileFullPath;
	}
	
	
	/*
	
	public byte[] record() throws IOException {
		final MediaRecorder mediaRecorder = new MediaRecorder();
		byte[] sound = new byte[1024];
		try {
			SensornetUtil.debug("SoundRecorder:record()");
			startRecord(mediaRecorder);
			Thread.sleep(duration);
			stopRecord(mediaRecorder);
		} catch (InterruptedException e) {
			throw new SensornetException(e, SensornetException.UNCONTROLED_ERROR,
					"Recording error. EXIT");
		}
		return sound;
	}

	
	private void startRecord(MediaRecorder mediaRecorder) throws IOException {
		try {
			Date date = new Date();
			//RECORDED FILE NAME
			fileName = date.getTime()+".3gp";
			String state = android.os.Environment
					.getExternalStorageState();
			if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
				throw new IOException("SD Card is not mounted.  It is "
						+ state + ".");
			}
			// make sure the directory we plan to store the
			// recording in exists
			File directory = new File(pathToFile).getParentFile();
			if (!directory.exists() && !directory.mkdirs()) {
				throw new IOException("Path to file created.");
			}
			mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
			mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
			mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
			mediaRecorder.setOutputFile(pathToFile + fileName);
			mediaRecorder.prepare();
			mediaRecorder.start();

		} catch (IOException ioe) {
			ioe.printStackTrace();
		};
	}

	
	private void stopRecord(MediaRecorder mediaRecorder) throws IOException {
		mediaRecorder.stop();
		mediaRecorder.release();
	}
*/
}
